﻿/*
 * fbasync - An Asynchronous Facebook API for .NET
 * Copyright (C) 2007 ittybittysoft
 * Email: info@ittybittysoft.com
 * Portions of the library are based on the Facebook Developer Toolkit
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Xml;

namespace fbasync
{
    [DataContract(), Serializable()]
    public class Event
        : FacebookEntity
    {
        public readonly static IEnumerable<string> AllFields =
            new string[] { "eid", "name", "tagline", "nid", "pic", "pic_big", "pic_small", "host", "description",
                           "event_type", "event_subtype", "start_time", "end_time", "creator", "update_time",
                           "location", "venue" };

        public const string DefaultOrderBy = "name";
        public const string FacebookEntity = "event";

        [DataMember]
        public long? eid { get; set; }

        [DataMember]
        public string name { get; set; }

        [DataMember]
        public string tagline { get; set; }

        [DataMember]
        public long? nid { get; set; }

        [DataMember]
        public string pic { get; set; }

        [DataMember]
        public string pic_big { get; set; }

        [DataMember]
        public string pic_small { get; set; }

        [DataMember]
        public string host { get; set; }

        [DataMember]
        public string description { get; set; }

        [DataMember]
        public string event_type { get; set; }

        [DataMember]
        public string event_subtype { get; set; }

        [DataMember]
        public DateTime? start_time { get; set; }

        [DataMember]
        public DateTime? end_time { get; set; }

        [DataMember]
        public long? creator { get; set; }

        [DataMember]
        public DateTime? update_time { get; set; }

        [DataMember]
        public string location { get; set; }

        [DataMember]
        public Venue venue { get; set; }

        public static IList<Event> FromFqlResult(XmlDocument fqlResult, bool keepXml)
        {
            List<Event> results = fqlResult.GetFqlResults(keepXml, e =>
                new Event
                {
                    creator = e.GetChildLong("creator"),
                    description = e.GetChildText("description"),
                    eid = e.GetChildLong("eid"),
                    end_time = e.GetChildUnixTime("end_time"),
                    event_subtype = e.GetChildText("event_subtype"),
                    event_type = e.GetChildText("event_type"),
                    host = e.GetChildText("host"),
                    location = e.GetChildText("location"),
                    name = e.GetChildText("name"),
                    nid = e.GetChildLong("nid"),
                    pic = e.GetChildText("pic"),
                    pic_big = e.GetChildText("pic_big"),
                    pic_small = e.GetChildText("pic_small"),
                    start_time = e.GetChildUnixTime("start_time"),
                    tagline = e.GetChildText("tagline"),
                    update_time = e.GetChildUnixTime("update_time"),
                    venue = new Venue(e["venue", FacebookConnection.FacebookNamespace])
                });
            return results;
        }

    }
}
